/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.DAO;
import GUI.FrameControlDanhMuc.JIFKhachHang;
import GUI.FrameControlDanhMuc.jframeThemKH;
import GUI.FrameControlDanhMuc.jframeTimSach;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Endlesslove
 */
public class KhachHang implements IDaTa
{
    //Lay thong tin khach hang theo ma khach hang
    public ResultSet GetInfo(String name)
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  name) ;//makhachhang
            return DAO.CallProc("LayThongTinKhachHang", params);
            
        } 
        catch (Exception ex) 
        {
            Logger.getLogger(jframeTimSach.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public ResultSet GetInfoTheoMa(String ma)
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  ma) ;//makhachhang
            return DAO.CallProc("LayThongTinKhachHangTheoMa", params);
            
        } 
        catch (Exception ex) 
        {
            Logger.getLogger(jframeTimSach.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
//    public String getName(String ID)
//    {
//        String result = "";
//        try 
//        {
//            ResultSet res = GetInfo(ID);
//            if(res.next())
//            {
//                
//                
//            }
//            
//        } catch (SQLException ex) {
//            Logger.getLogger(KhachHang.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return result;
//    }
    @Override
    public ResultSet Search(DTO.Idata Obj) 
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  ((DTO.KhachHang)Obj).getID()) ;//makhachhang
            params.add("nvarchar:" +  ((DTO.KhachHang)Obj).TenhachHang);//tenkhachhang
            params.add("nvarchar:" + ((DTO.KhachHang)Obj).DiaChi );//diachi
            params.add("nvarchar:" +  ((DTO.KhachHang)Obj).Email );//Email
            params.add("nvarchar:" +  ((DTO.KhachHang)Obj).DienThoai);//DienThoai
            params.add("smallint:" + ((DTO.KhachHang)Obj).TienNo );
            params.add("smallint:" + ((DTO.KhachHang)Obj).TienNo2 );            
            params.add("char:OR" );//Lua chon ti iem theo OR hoac And
            return DAO.CallProc("TimKhachHang", params);
            
        } 
        catch (Exception ex) 
        {
            Logger.getLogger(JIFKhachHang.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    @Override
    public boolean Update(DTO.Idata Obj)
    {
        List<String> params = new ArrayList<>();
        params.add("char:" +  ((DTO.KhachHang)Obj).getID()) ;//makhachhang
        params.add("nvarchar:" +  ((DTO.KhachHang)Obj).TenhachHang);//tenkhachhang
        params.add("nvarchar:" +  ((DTO.KhachHang)Obj).DiaChi );//diachi
        params.add("nvarchar:" +  ((DTO.KhachHang)Obj).Email );//Email
        params.add("nvarchar:" +  ((DTO.KhachHang)Obj).DienThoai);//DienThoai
        params.add("int:" + ((DTO.KhachHang)Obj).TienNo);//TienNo
        return DAO.CallProc("SuaKhachHang", params, true);
    }

    @Override
    public boolean Delete(DTO.Idata Obj)
    {
        List<String> params = new ArrayList<>();
        params.add("char:" + ((DTO.KhachHang)Obj).getID());
        return DAO.CallProc("XoaKhachHang", params, true);
    }

    @Override
    public void LoadData(Object table) 
    {
        try
        {
            ResultSet res = DAO.CallProc("LayThongTinToanBoKhachHang");
            int count = 1;
            while (res.next()) 
            {
                ((DefaultTableModel)((JTable)table).getModel()).addRow(new Object[] 
                {
                    count++ , res.getObject(1), res.getObject(2), res.getObject(3), res.getObject(4), res.getObject(5),res.getObject(6)
                });
            }
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(JIFKhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }  
    }

    @Override
    public boolean Insert(DTO.Idata Obj) 
    {
       List<String> params = new ArrayList<>();
        params.add("char:" +  ((DTO.KhachHang)Obj).getID()) ;//makhachhang
        params.add("nvarchar:" +  ((DTO.KhachHang)Obj).TenhachHang);//tenkhachhang
        params.add("nvarchar:" +  ((DTO.KhachHang)Obj).DiaChi );//diachi
        params.add("nvarchar:" +  ((DTO.KhachHang)Obj).Email );//Email
        params.add("nvarchar:" +  ((DTO.KhachHang)Obj).DienThoai);//DienThoai
        params.add("int:" + ((DTO.KhachHang)Obj).TienNo);//TienNo
        return DAO.CallProc("ThemKhachHang", params, true);
    }
    
    @Override
    public String GenerateID()
    {
        try 
        {
                ResultSet res = DAO.CallProc("LayMaKhachHangCuoi");
                if (res.next())
                {    
                  int index  = Integer.parseInt(((String)res.getObject(1)).split("KH")[1].trim());
                  String mkh = "KH";
                  mkh += (index+1)>=10?(index+1)>=100? (index +1) : "0" + (index +1) : "00" + (index + 1);
                  return mkh;
                }
                return null;
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(jframeThemKH.class.getName()).log(Level.SEVERE, null, ex);
            return "KH001";
        }
        catch(Exception ie)
        {
             return "KH001";
        }
    }
    
    /**
     *
     * @return Bang chua thong tin toan bo khach hang
     * makhachhang, ten khach hang, dia chi. email. dien thoai
     */
    public ResultSet GetAll()
    {
         try
        {
            return DAO.CallProc("LayThongTinToanBoKhachHang");
        } 
        catch (Exception ex) 
        {
            Logger.getLogger(jframeTimSach.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
